import util from './utils'
import styles from './style'
import polygon from './polygon'
import { config as sysconfig } from './config'
import { Vector as VectorSource } from 'ol/source'
import Draw from 'ol/interaction/Draw'
import Collection from 'ol/Collection'
import { Vector as VectorLayer } from 'ol/layer'
import Select from 'ol/interaction/Select'
import { altKeyOnly, click, pointerMove } from 'ol/events/condition'

const draw = {
    map: null, // 地图对象
    draw: null, // 绘制对象
    select: null, // 选择对象
    vector: null, // 绘制图层
    source: null, // source对象
    selectVector: null, // 选择图层
    feature: [], // 绘制feature
    pointerMoveObj: null, // 鼠标跟随标记
    // layer图层对象，style样式，lonlats经纬度，attrs自定义属性
    // type = Point  LineString  Polygon  Circle
    /**
     * @param {Object} map 地图对象
     * @param {Object} style 样式对象
     * @param {String} type 绘制类型
     * @param {Object} attrs 自定义属性
     */
    init: function (map, style, type, attrs = {}) {
        style = styles.init(style)[0]
        if (!this.source) {
            this.source = new VectorSource({
                features: new Collection(),
                wrapX: false
            })
        }
        let draw = new Draw({
            source: this.source,
            type: type,
            style: style,
            stopClick: true
        })
        map.addInteraction(draw)
        draw.on('drawend', (e) => {
            console.log(e)
            e.feature.set('name', 'e-draw')
            e.feature.set('geomName', type)
            this.feature.push(e.feature)
        })
        if (!this.vector) {
            this.vector = this.addDrawToVector(map, this.source, style)
        }
        this.draw = draw
        this.map = map
        // let layer = this.addDrawToVector(map, source, style);
        // this.draw = draw;
        //       this.map = map;
        //       this.vector = layer;
        return { draw: draw, layer: this.vector }
    },
    // 激活绘制
    reDraw: function () {
        console.log(this.map && this.draw)
        if (this.map && this.draw) {
            this.map.addInteraction(this.draw)
        }
    },
    // 添加绘制到图层
    addDrawToVector: function (map, source, style) {
        const vector = new VectorLayer({
            source: source,
            style: style
        })
        vector.setZIndex(1000)
        map.addLayer(vector)
        return vector
    },
    startDraw(map) {
        map.addInteraction(this.draw)
    },
    stopDraw(map) {
        map.removeInteraction(this.draw)
    },
    // 清除所有绘制
    clearAllDraw: function () {
        this.vector.getSource().clear()
    },
    // 清除上一个
    clearbrfeforeDraw: function () {
        console.log(this.feature, this.vector)
        if (this.feature.length > 0 && this.vector) {
            let feature = this.feature.pop()
            this.vector.getSource().removeFeature(feature)
        }
    },
    // 选择
    initSelect(layers, style) {
        // 创建选择图层
        // 创建select对象
        style = styles.init(style)[0]
        let select = new Select({
            condition: pointerMove,
            style: style,
            layers: layers
        })
        this.select = select
        return { select: select, layer: layers }
    },
    // 开始选择
    startSelect(map, cb) {
        map.addInteraction(this.select)
        // 监听select事件
        this.select.on('select', function (e) {
            cb && cb(e)
        })
    },
    // 绘制鼠标跟随标记
    pointerMove(layer, lonlats, style) {
        if (this.pointerMoveObj) {
            this.clearPointerMove(layer, this.pointerMoveObj)
            this.pointerMoveObj = null
        }
        if (!style) {
            style = {
                isIcon: true,
                icon: {
                    src: require('../../assets/images/point.png'),
                    size: [32, 32],
                    scale: 0.8,
                    offset: [0, 0]
                }
            }
        }
        this.pointerMoveObj = polygon.drawPolygon(layer, style, lonlats, 'point')
        return this.pointerMoveObj
    },
    clearPointerMove(layer, feature) {
        if (layer && feature) {
            layer.getSource().removeFeature(feature)
        }
    }
}

export default draw
